/**
 * Created by liaoxin on 2017-08-30.
 */

$(function(){
    //treeview 初始化
    initTreeViewMenu();

    //关闭弹出框时清空内容

    $('#popwin').on('hidden.bs.modal', function () {
        clearPopMenuAdd();
    })

    $('#popchildwin').on('hidden.bs.modal', function () {
        clearPopMenuChildAdd();
    })
    
    $('#popApporval').on('show.bs.modal', function () {
        $("#menuCodeEdit").val($("#menuCodeShow").text());
        $("#menuNameEdit").val($("#menuNameShow").text());
        $("#menuUrlEdit").val($("#menuUrlShow").text());
        $("#menuSortEdit").val($("#menuSortShow").text());
    })

});

/**
 * 关闭弹窗后清空数据
 */
function clearPopMenuAdd() {
    $("#menuNameAdd").val("");
    $("#menuUrlAdd").val("");
    $("#menuSortAdd").val("");
    $("#menuBelongAdd").val("1");//默认值
}

function clearPopMenuChildAdd() {
    $("#menuNameChildAdd").val("");
    $("#menuUrlChildAdd").val("");
    $("#menuSortChildAdd").val("");
    $("#menuBelongChildAdd").val("1");//默认值
}


function hideViewPop(operationType) {
    if (operationType == 'add') {
        $("#popwin").modal('hide');
    } else if(operationType == 'addChild'){
        $('#popchildwin').modal('hide');
    }else if (operationType == 'edit'){
        $('#popApporval').modal('hide');
    }else if (operationType == 'editChild'){
        $('#popApporvalChild').modal('hide');
    }

}

/**
 * treeview 初始化
 */
function initTreeViewMenu() {
    //初始化
    initClassifyManagerSelectMenuData('menuManagerTreeView');
}

function initClassifyManagerSelectMenuData(weigetId) {
    initTreeView(weigetId,staticMenuData,5);
}


/**
 *
 * @param widgetId
 * @param treeData
 * @param showlevels
 */
function initTreeView(widgetId,treeData,showlevels,loadAddNodeMethod){
    var treeShowlevels = 5;
    if(null!=showlevels || ""!=showlevels){
        treeShowlevels = showlevels;
    }
    var control  = $('#' + widgetId);
    control.treeview({
        data: treeData,
        silent: true,
        levels: treeShowlevels,   //展示层级控制
        multiSelect: false,  //多选
        onNodeSelected: function(event, node) { //选中节点
            if(node.state.expanded){
	            
            } else {
                //展开
                control.treeview('expandNode', node.nodeId);
            }
            $("#menuChildList").show();
            itemClick(node);

        },
        onNodeUnselected: function(event, node) {
            /*
             if(node.state.expanded){
             //处于展开状态则折叠
             control.treeview('collapseNode', node.nodeId);
             } else {
             //展开
             control.treeview('expandNode', node.nodeId);
             }*/
            // $("#catInfoTab").hide();
            $("#menuChildList").hide();
            $("#menuCodeShow").html("");
            $("#menuNameShow").html("");
            $("#menuTypeNameShow").html("");
            $("#menuUrlShow").html("");
            $("#menuBelongNameShow").html("");
            $("#menuSortShow").html("");
            $("#parentMenuCode").val("");
        },
        onNodeExpanded:
            function(event, data) {
            }
    });
}

/**
 * 菜单数节点点击事件
 * @param clickedNode
 */
function itemClick(clickedNode) {
    $("#menuCodeShow").html(clickedNode.id);
    $("#menuNameShow").html(clickedNode.text);
    $("#menuTypeNameShow").html(clickedNode.typeName);
    $("#menuUrlShow").html(clickedNode.url);
    $("#menuBelongNameShow").html(clickedNode.belong);
    $("#menuSortShow").html(clickedNode.sortOrder);
    $("#parentMenuCode").val(clickedNode.selfParentId);
}


function  showMenuButton(menuCode) {
    var params = {
        parentMenuCode:menuCode,
    };
    var url = WEB_ROOT +"/menu/queryMenuChildInfo";
    $.ajax({
        url : url,
        data : params,
        dataType : "json",
        type : "POST",
        async : true,
        success : function (data) {
            if (data.menuFlag == "1") {
                $("#addMenuChild").hide();
                $("#addMenuChildButton").show();
            }else if (data.menuFlag == "0") {
                $("#addMenuChild").show();
                $("#addMenuChildButton").hide();
            }else if (data.menuFlag == "2") {
                $("#addMenuChild").show();
                $("#addMenuChildButton").show();
            }
        }
    });
}

/**
 * 新增按钮
 */
function popAddMenu() {
    if ($("#menuTypeShow").text() == '2') {
        $("#popchildwin").modal('show');
    } else {
        $("#popwin").modal('show');
    }
}

/**
 * 新增菜单
 */
function addMenuInfo() {
	$("#popwin").modal('hide');
	WEB.msg.info("提示","新增菜单成功",function () {
        location.reload();
    });
                
    return ;
}
/**
 *
 * 新增子菜单保存
 */
function addMenuChildInfo() {
	$('#popchildwin').modal('hide');
	WEB.msg.info("提示","新增菜单成功",function () {
                    //location.reload();
                });
    return;
}
/**
 * 子菜单新增
 */
function popAddChildMenu(operation) {
    $("#menuTypeChildAdd").empty();
    if (operation == 1) {
        $("#menuTypeChildAdd").append("<option value=\"0\" >菜单</option>");
    }else {
        $("#menuTypeChildAdd").append("<option value=\"1\" >按钮</option>");
    }
    $("#popchildwin").modal('show');
}

/**
 * 菜单编辑
 */
function popUpdateMenu() {
    $("#popApporval").modal('show');
}

/**
 * 子菜单编辑
 */
function operateMenuInfo(menuCode,menuName,menuUrl,menuSort) {
    $('#menuNameChildEdit').val(menuName);
    $('#menuCodeChildEdit').val(menuCode);
    $('#menuUrlChildEdit').val(menuUrl);
    $('#menuSortChildEdit').val(menuSort);
    $("#popApporvalChild").modal('show');
}

/**
 * 菜单编辑保存
 */
function updateMenuInfo() {
	$("#popApporval").modal('hide');
	WEB.msg.info("提示","编辑菜单成功",function () {
        location.reload();
    });
                
    return ;
}

/**
 * 下级菜单编辑保存
 */
function updateMenuChildInfo() {
	$("#popApporvalChild").modal('hide');
	WEB.msg.info("提示","编辑菜单成功",function () {
                    location.reload();
                });
    return;
}


/**
 * 删除菜单
 */
function delMenuInfo(clickId) {
	WEB.msg.confirm('提示','请确认要删除该菜单吗？',function () {
		WEB.msg.info("提示","删除菜单成功",function () {
                        location.reload();
                    });
        })
    
    return;


}